package com.wyzz.hr.dao.impl;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

public class BackUpTools {

	public BackUpTools() {
	}

	public static boolean execSqlCmd(String mysqlBinPath, String filePath,
			String sqlCmdStr) {
		if (mysqlBinPath == null || filePath == null) {
			logger.info((new StringBuilder()).append(
					"mysqlBinPath == null || filePath == null //").append(
					mysqlBinPath).append("--").append(filePath).toString());
			return false;
		}
		File file = new File(filePath.substring(0, filePath.lastIndexOf("/")));
		if (!file.exists())
			file.mkdirs();
		filePath = filePath.replaceAll(" ", "\" \"");
		if (sqlCmdStr == null || sqlCmdStr.length() == 0) {
			logger.info("sql command == null");
			return false;
		}
		mysqlBinPath = mysqlBinPath.replaceAll(" ", "\" \"");
		try {
			String COMMAND_LAST[] = new String[3];
			if (System.getProperty("os.name").indexOf("Windows") != -1) {
				COMMAND_LAST[0] = "cmd ";
				COMMAND_LAST[1] = "/c ";
				COMMAND_LAST[2] = (new StringBuilder()).append(mysqlBinPath)
						.append(sqlCmdStr).append(filePath).toString();
				logger.info((new StringBuilder()).append("Execute:: ").append(
						COMMAND_LAST[0]).append(COMMAND_LAST[1]).append(
						COMMAND_LAST[2]).toString());
				Process process = Runtime.getRuntime().exec(
						(new StringBuilder()).append(COMMAND_LAST[0]).append(
								COMMAND_LAST[1]).append(COMMAND_LAST[2])
								.toString());
				process.waitFor();
			} else {
				COMMAND_LAST[0] = "/bin/sh";
				COMMAND_LAST[1] = "-c";
				COMMAND_LAST[2] = (new StringBuilder()).append(mysqlBinPath)
						.append(sqlCmdStr).append(filePath).toString();
				logger.info((new StringBuilder()).append("Execute:: ").append(
						COMMAND_LAST[0]).append(COMMAND_LAST[1]).append(
						COMMAND_LAST[2]).toString());
				Process process = Runtime.getRuntime().exec(COMMAND_LAST);
				process.waitFor();
			}
		} catch (Exception e) {
			logger.info((new StringBuilder()).append("e//").append(e)
					.toString());
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * @throws IOException
	 * @throws InterruptedException
	 * @deprecated Method resume is deprecated
	 */

	public static boolean resume(String mysqlBinPath, String filePath,
			String dataName, String userName, String password)
			throws IOException, InterruptedException {
		if (mysqlBinPath == null || filePath == null) {
			logger.info((new StringBuilder()).append(
					"mysqlBinPath == null || filePath == null //").append(
					mysqlBinPath).append("--").append(filePath).toString());
			return false;
		}
		if (dataName == null || userName == null || password == null) {
			logger
					.info((new StringBuilder())
							.append(
									"dataName == null || userName == null || password == null//")
							.append(dataName).append("--").append(userName)
							.append("++").append(password).toString());
			return false;
		}
		String COMMAND_TAIL[];
		Process process;
		try {
			File sqlFile = new File(filePath);
			if (!sqlFile.exists()) {
				logger.info((new StringBuilder()).append("filePath//").append(
						filePath).toString());
				return false;
			}
		} catch (Exception e) {
			logger.info((new StringBuilder()).append("e//").append(e)
					.toString());
			return false;
		}
		filePath = filePath.replaceAll(" ", "\" \"");
		mysqlBinPath = mysqlBinPath.replaceAll(" ", "\" \"");
		COMMAND_TAIL = new String[3];
		if (System.getProperty("os.name").indexOf("Windows") != -1) {
			COMMAND_TAIL[0] = "cmd ";
			COMMAND_TAIL[1] = "/c ";
			COMMAND_TAIL[2] = (new StringBuilder())
					.append(mysqlBinPath)
					.append(
							"/mysql --default-character-set=utf8 -hlocalhost -u")
					.append(userName).append(
							password == null || password.equals("") ? ""
									: (new StringBuilder()).append(" -p")
											.append(password).toString())
					.append(" ").append(dataName).append(" < ")
					.append(filePath).toString();
			process = Runtime.getRuntime()
					.exec(
							(new StringBuilder()).append(COMMAND_TAIL[0])
									.append(COMMAND_TAIL[1]).append(
											COMMAND_TAIL[2]).toString());
			process.waitFor();
		} else {
			COMMAND_TAIL[0] = "/bin/sh";
			COMMAND_TAIL[1] = "-c";
			COMMAND_TAIL[2] = (new StringBuilder())
					.append(mysqlBinPath)
					.append(
							"/mysql --default-character-set=utf8 -hlocalhost -u")
					.append(userName).append(
							password == null || password.equals("") ? ""
									: (new StringBuilder()).append(" -p")
											.append(password).toString())
					.append(" ").append(dataName).append(" < ")
					.append(filePath).toString();
			logger.info((new StringBuilder()).append("COMMAND_LAST//").append(
					COMMAND_TAIL[0]).append(COMMAND_TAIL[1]).append(
					COMMAND_TAIL[2]).toString());
			process = Runtime.getRuntime().exec(COMMAND_TAIL);
			process.waitFor();
		}
		return true;
	}

	static Logger logger = Logger.getLogger(com.wyzz.hr.pojo.Backup.class);

}
